package com.example.shaoying.yuejian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.activity.ShaiTuActivity;
import com.example.shaoying.yuejian.activity.UpdateUserActivity;
import com.example.shaoying.yuejian.adapter.PhotoAdapter;
import com.example.shaoying.yuejian.javabean.ShaiTuList;
import com.example.shaoying.yuejian.model.PhotoModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class PictureFragment extends Fragment {

    private RecyclerView recyclerView;
    private PhotoAdapter adapter;
    private View view;
    private ImageView shaitu;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       view= inflater.inflate(R.layout.tab04, container, false);
       recyclerView= (RecyclerView) view.findViewById(R.id.shaitu_list);
        shaitu= (ImageView) view.findViewById(R.id.shaitu);
        shaitu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), ShaiTuActivity.class);
                getContext().startActivity(intent);
            }
        });
        final StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

        PhotoModel model =new PhotoModel();
        Call<List<ShaiTuList>> call=model.getshaituList();
        call.enqueue(new Callback<List<ShaiTuList>>() {
            @Override
            public void onResponse(Call<List<ShaiTuList>> call, Response<List<ShaiTuList>> response) {
                List<ShaiTuList> lists=response.body();
                adapter=new PhotoAdapter(getActivity(),lists);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(adapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFailure(Call<List<ShaiTuList>> call, Throwable t) {

                Toast.makeText(getActivity(),"获取列表失败",Toast.LENGTH_SHORT).show();

            }
        });
        return view;
    }


}
